// pages/releaseS/releaseS.js
const {
	tribetypelist,
	addtribe
} = require('../../utils/api')
const { fetch } = require('../../utils/fetch')

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list:[],
		logo_img:[],
		specificarea:null,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if(wx.getStorageSync('specificarea')) {
			this.setData({
				specificarea:wx.getStorageSync('specificarea')
			})
		}

		fetch(`wx/tribe/${this.data.specificarea}/tribetypelist`, 'POST').then(res => {
			var list = [];
			res.forEach((value,index)=>{
				var idx = index + 1 
				list.push(value[idx])
			})
			this.setData({
				list: list
			})
		})
	},
	showToast: function (title) {
		wx.showToast({
			title,
			icon: 'none',
			duration: 1000
		})
	},
	bindPickerChange: function (e) {
		this.setData({
			index: e.detail.value
		})
	},
	bind_logo: function (e) {
		this.data.logo_img = e.detail.imagsetentlogs;
	},
	formSubmit(e){
		if (!this.data.list[this.data.index]){
			this.showToast('请选择部落类别');
			return;
		} else if (!e.detail.value.tribeName) {
			this.showToast('请填写部落名称');
			return;
		} else if (!e.detail.value.tribeContent) {
			this.showToast('请填写部落介绍');
			return;
		} else if (this.data.logo_img.length == 0) {
			this.showToast('请上传封面');
			return;
		}else{
			fetch(`wx/tribe/${this.data.specificarea}/addtribe`, 'POST', {
				tribetype: this.data.index * 1 + 1,
				tribetitle: e.detail.value.tribeName,
				tribedesc: e.detail.value.tribeContent,
				area: this.data.specificarea,
				memeberid: wx.getStorageSync('userInfo').id,
				tribehead: this.data.logo_img[0]
			}).then(res=>{
				wx.navigateBack({
					delta:1
				})
			})
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})